<?php
require_once('../../config.php');
Doctrine::loadModels('../../models');

// need to evolve search
if( $_GET["project_id"])
{
	$project_id = strip_tags($_GET["project_id"]);	
	file_put_contents  ( "param_search_str.txt", $project_id);
	
	$query = new Doctrine_Query();
   	$query->from('FinProfitLoss')
				->where('deleted = 0 
							AND project_id ='.$project_id)
            	->orderby('org_fin_centre_id');
                  
   	$items = $query->execute();	   
   	file_put_contents  ( "select_by_centre.txt", $items);
}
else if ($_GET["org_fin_centre_id"]) 
{
	$org_fin_centre_id = strip_tags($_GET["org_fin_centre_id"]);	
	file_put_contents  ( "param_search_str.txt", $org_fin_centre_id);
	
	$query = new Doctrine_Query();
   	$query->from('FinProfitLoss')
				->where('deleted = 0 
							AND org_fin_centre_id ='.$org_fin_centre_id)
            	->orderby('project_id');
                  
   	$items = $query->execute();
   	file_put_contents  ( "select_by_centre.txt", $items);
}
else 
{	
	$query = new Doctrine_Query();
    $query->from('FinProfitLoss')
    			->where('deleted = 0')
            	->orderby('fin_year_id');
                  
    $items = $query->execute();	
    file_put_contents  ( "select_all.txt", $items);
}
            
/// Return XML
$str.='<results>';
	foreach ($items as $item){
		$str.='<record>';
			$str.='<profit_loss_id>'.$item["profit_loss_id"].'</profit_loss_id>';
			$str.='<project_id>'.$item["project_id"].'</project_id>';
			$str.='<fin_year_id>'.$item["fin_year_id"].'</fin_year_id>';
			$str.='<rev_exp_id>'.$item["rev_exp_id"].'</rev_exp_id>';
			$str.='<org_fin_centre_id>'.$item["org_fin_centre_id"].'</org_fin_centre_id>';
			$str.='<m01_budget>'.$item["m01_budget"].'</m01_budget>';
			$str.='<m02_budget>'.$item["m02_budget"].'</m02_budget>';
			$str.='<m03_budget>'.$item["m03_budget"].'</m03_budget>';
			$str.='<m04_budget>'.$item["m04_budget"].'</m04_budget>';
			$str.='<m05_budget>'.$item["m05_budget"].'</m05_budget>';
			$str.='<m06_budget>'.$item["m06_budget"].'</m06_budget>';
			$str.='<m07_budget>'.$item["m07_budget"].'</m07_budget>';
			$str.='<m08_budget>'.$item["m08_budget"].'</m08_budget>';
			$str.='<m09_budget>'.$item["m09_budget"].'</m09_budget>';
			$str.='<m10_budget>'.$item["m10_budget"].'</m10_budget>';
			$str.='<m11_budget>'.$item["m11_budget"].'</m11_budget>';
			$str.='<m12_budget>'.$item["m12_budget"].'</m12_budget>';
			$str.='<m01_actual>'.$item["m01_actual"].'</m01_actual>';
			$str.='<m02_actual>'.$item["m02_actual"].'</m02_actual>';
			$str.='<m03_actual>'.$item["m03_actual"].'</m03_actual>';
			$str.='<m04_actual>'.$item["m04_actual"].'</m04_actual>';
			$str.='<m05_actual>'.$item["m05_actual"].'</m05_actual>';
			$str.='<m06_actual>'.$item["m06_actual"].'</m06_actual>';
			$str.='<m07_actual>'.$item["m07_actual"].'</m07_actual>';
			$str.='<m08_actual>'.$item["m08_actual"].'</m08_actual>';
			$str.='<m09_actual>'.$item["m09_actual"].'</m09_actual>';
			$str.='<m10_actual>'.$item["m10_actual"].'</m10_actual>';
			$str.='<m11_actual>'.$item["m11_actual"].'</m11_actual>';
			$str.='<m12_actual>'.$item["m12_actual"].'</m12_actual>';
			$str.='<created>'.$item["created"].'</created>';
		$str.='</record>';
	}
$str.='</results>';
   
header ("content-type: text/xml"); 
echo $str;

file_put_contents  ( "getAgent_Out.txt", $str);

?>